package mylibrary.spider.framework.task;

import java.util.concurrent.BlockingQueue;

import mylibrary.spider.framework.Crawler;
import mylibrary.spider.framework.Page;

public class CrawlTask implements Runnable {

	Crawler crawler = new Crawler();
	
	private BlockingQueue<String> detectedUrl;
	
	public CrawlTask(BlockingQueue<String> detectedUrl) {
		this.detectedUrl = detectedUrl;
	}
	
	public void run() {
		
		while(true) {
			try {
				
				System.out.println("------------------------------------------------" );
				System.out.println(Thread.currentThread().getName() + " is in " );
				String url = detectedUrl.take();
				
				
				if(!Crawler.handlingUrl.contains(url)) {
					System.out.println(Thread.currentThread().getName() + " is handling " + url);
					
					Page page = new Page(url, null);
					crawler.crawl(page);
				}
				
				
				System.out.println("------------------------------------------------" );
				
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		
	}

}
